package biz

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func CreateHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		name := c.PostForm("name")
		hero := CreateHero(name)
		c.JSON(http.StatusOK, gin.H{"hero": hero})
	}
}

func GetHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		IDStr := c.Param("id")
		ID, _ := strconv.ParseInt(IDStr, 10, 64)
		hero := GetHero(ID)
		c.JSON(http.StatusOK, gin.H{"hero": hero})
	}
}

func UpdateHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		IDStr := c.PostForm("id")
		ID, _ := strconv.ParseInt(IDStr, 10, 64)
		name := c.PostForm("name")
		hero := GetHero(ID)
		hero.Name = name
		hero.Update()
		c.JSON(http.StatusOK, gin.H{"hero": hero})
	}
}

func DeleteHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		IDStr := c.PostForm("id")
		ID, _ := strconv.ParseInt(IDStr, 10, 64)
		hero := GetHero(ID)
		hero.Del()
		c.JSON(http.StatusOK, gin.H{"hero": hero})
	}
}
